package com.dayi.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dayi.R;

import java.util.List;

public class MySpinner extends LinearLayout {

    private TextView tv_value;

    private int mNormalColor;
    private int mSelectedColor;

    private Context mcontext;
    private List<String> mItems;
    OnItemSelectedListener listener;
    private SpinnerPopWindow mSpinerPopWindow;
    private SpinnerListAdapter mAdapter;
    View myView;


    public MySpinner(Context context) {
        super(context);
        mcontext = context;
        init();
    }

    public MySpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        mcontext = context;
        init();
    }

    private void init() {
        LayoutInflater mInflater = LayoutInflater.from(mcontext);
        myView = mInflater.inflate(R.layout.myspinner_layout, null);
        addView(myView);

        tv_value = (TextView) myView.findViewById(R.id.tv_grade);

        tv_value.setOnClickListener(onClickListener);
    }

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
//            bt_dropdown.setBackgroundResource(R.drawable.up_arrow);
            startPopWindow();
        }
    };

    public void setData(List<String> datas) {
        mItems = datas;
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.listener = listener;
    }


    public void startPopWindow() {

        mSpinerPopWindow = new SpinnerPopWindow(mcontext, mItems);
        mSpinerPopWindow.setSpinnerItemClickListener(new SpinnerPopWindow.SpinnerItemClickListener() {
            @Override
            public void itemClick(String itemText) {
                tv_value.setText(itemText);
            }
        });
        showSpinWindow();
    }

    private void showSpinWindow() {
        mSpinerPopWindow.setWidth(myView.getWidth());
        mSpinerPopWindow.showAsDropDown(myView);
    }

    public interface OnItemSelectedListener {
        void onItemSelected(int pos);
    }


}
